/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package foalvleditor.core.impl;

import foalvleditor.ctrl.BaseController;
import foalvleditor.gameobjects.AbstractGameObject;
import java.util.ArrayList;

/**
 *
 * @author Mikael
 */
public final class UndoModel {

	private ArrayList<ArrayList<AbstractGameObject>> undoObjects=
		new ArrayList<ArrayList<AbstractGameObject>>();
    private ArrayList<AbstractGameObject> undoScenery=
    	new ArrayList<AbstractGameObject>();
    private ArrayList<String> undoName=new ArrayList<String>();

    private static UndoModel instance=new UndoModel();

    public static UndoModel getInstance(){
        return instance;
    }
    public void undo(){
        if (undoObjects!=null)
        {
        	BaseController.levelModel.setLevel((ArrayList<AbstractGameObject>)undoObjects.get(0));
            undoObjects.remove(0);
        }
        if (undoScenery!=null)
        {
        	BaseController.levelModel.setBackground((AbstractGameObject) undoScenery.get(0));
            undoScenery.remove(0);
        }
        if (undoName!=null)
        {
        	BaseController.levelModel.setName((String)undoName.get(0));
            undoName.remove(0);
        }
        BaseController.levelModel.getView().repaintAllPanels();
        BaseController.levelModel.getView().repaintAllEvents();
        BaseController.levelModel.getView().repaintAll();
    }

    public boolean undoPossible(){
        return (undoObjects!=null);
    }

    public void addUndoElement(){


       ArrayList<AbstractGameObject> temp=new ArrayList<AbstractGameObject>();
       for (int i=0; i<BaseController.levelModel.getLevel().size(); i++)
       {
           AbstractGameObject addobj;
           try{
                addobj=BaseController.levelModel.getLevel().get(i).clone();
           }
           catch (CloneNotSupportedException e){
                addobj=BaseController.levelModel.getLevel().get(i);
           }
        temp.add(0,addobj);
       }
       undoObjects.add(0, temp);
       undoScenery.add(0, BaseController.levelModel.getBackground());
       undoName.add(0, BaseController.levelModel.getName());
    }

 }


